package pong;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Rectangle;

import states.SlickGame;
import globals.DataTracker;
import interfaces.Controllable;
import interfaces.Trackable;

public class Paddle extends Controllable {
	//public boolean isUp, isDown // Current State
	//public float x,y,r;
	// Object Data
	public Color colorMe=Color.red;
	public boolean collisionLeft;
	
	// Global Data
	public static int dx=26;
	public static int dy=76;
	public static int speed=10;
	
	public Paddle(int xin,int yin,boolean left){
		super();	collisionLeft=left;
		x=xin;	y=yin;	r=dy/2;
	}
	
	public void update(int delta){
		if(isUp){ y-=speed;}
		else if(isDown){ y+=speed;}
		
		if(y+dy/2 > SlickGame.height){y=SlickGame.height-dy/2;}
		if(y-dy/2 < 0){y=dy/2;}
	}
	
	public void checkCollision(Ball object){
		if(collisionLeft){ // if expecting a collision from left
			if(object.x>=x && object.x<x+dx && object.y>y-dy/2 && object.y<y+dy/2){
				object.direction.setX(object.direction.getX()*-1);	object.x-=object.r;
				object.direction.y+=(object.y/(y+dy/6));
			}
		}
		else{
			if(object.x<=x && object.x>x-dx && object.y>y-dy/2 && object.y<y+dy/2){
				object.direction.setX(object.direction.getX()*-1);	object.x+=object.r;
				object.direction.y+=(object.y/(y+dy/6));
			}
		}
	}
	
	public void draw(Graphics g){
		if(collisionLeft){
			g.setColor(colorMe);
			g.fill(new Rectangle(x,y-dy/2,10,dy));
			g.setColor(colorMe.darker());
			g.draw(new Rectangle(x,y-dy/2,10,dy));
		}else{
			g.setColor(colorMe.brighter());
			g.fill(new Rectangle(x,y-dy/2,-10,dy));
			g.setColor(colorMe);
			g.draw(new Rectangle(x,y-dy/2,-10,dy));
		}
	}

	public void specialUpdate(Trackable object, float error){
		int maxFrameTest=100;
		Ball futureBall = ((Ball) object).getCopy();
		for(int i=0; i<maxFrameTest; i++){
			if(collisionLeft && futureBall.x > x){break;}
			if(!collisionLeft && futureBall.x < x){break;}
			
			futureBall.update(0);
		}
		
		// Control Y - Up/Down
		if(futureBall.y+futureBall.r*error>y){setDown(true);}
		else{setDown(false);}
		
		if(futureBall.y-futureBall.r*error<y){setUp(true);}
		else{setUp(false);}
	}
}
